using System;
using System.Web;
using System.Collections.Generic;


namespace CreatureSoft.Hoot.Web {

    public class MonitorAspNetApplicationModule : IHttpModule
    {
        public void Init(HttpApplication application)
        {
            application.EndRequest += (new EventHandler(this.Application_EndRequest));
        }

        private void Application_EndRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            if (context.Items[AppStart_RegisterMonitoring._item] != null)
            {
                //Only add the request if atleast one exception is raised
                var reqExc = context.Items[AppStart_RegisterMonitoring._item] as RequestException;
                reqExc.Url = context.Request.Url.AbsoluteUri;
                context.Application.Lock();
                if (context.Application["AllExc"] == null)
                    context.Application["AllExc"] = new List<RequestException>();
                (context.Application["AllExc"] as List<RequestException>).Add(reqExc);
                context.Application.UnLock();
            }
        }

        public void Dispose()
        {
        }

    }    
}